package org.jeecg.modules.flowermater.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.jeecg.modules.flower.entity.FlowerAcont;
import org.jeecg.modules.flower.entity.FlowerStorehouse;
import org.jeecg.modules.flower.mapper.FlowerStorehouseMapper;
import org.jeecg.modules.flowermater.entity.*;
import org.jeecg.modules.flowermater.mapper.*;
import org.jeecg.modules.flowermater.service.IFlowerMaterialCountService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 物资统计
 */
@Service
@Log4j2
public class FlowerMaterialCountServiceImpl extends ServiceImpl<FlowerMaterialCountMapper, FlowerMaterialCount> implements IFlowerMaterialCountService {
    @Autowired
    private FlowerStorehouseMapper flowerStorehouseMapper;
    @Autowired
    private ISysDictService sysDictService;

    /**
     * 物资盘点
     *
     * @param storehouse
     * @param cost
     * @param sDate
     * @param eDate
     * @return
     */
    @Override
    public List<FlowerMaterialCount> queryMaterCount(String storehouse, String cost, String sDate, String eDate) {
        //上期入库
        List<FlowerMaterialCount> queryBeforInList = baseMapper.queryBeforIn(storehouse, cost, sDate, eDate);
        //上期出库
        List<FlowerMaterialCount> queryBeforoutList = baseMapper.queryBeforout(storehouse, cost, sDate, eDate);

        //本期入库
        List<FlowerMaterialCount> queryNowInList = baseMapper.queryNowIn(storehouse, cost, sDate, eDate);
        //本期出库
        List<FlowerMaterialCount> queryNowOutList = baseMapper.queryNowOut(storehouse, cost, sDate, eDate);

        Map<String, FlowerMaterialCount> map = new HashMap<>();
        for (FlowerMaterialCount flowerMaterialBeforIn : queryBeforInList) {
            BigDecimal zero = BigDecimal.ZERO;
            flowerMaterialBeforIn.setBennum( zero );
            flowerMaterialBeforIn.setBenprice( zero );
            flowerMaterialBeforIn.setBenprice( zero );
            flowerMaterialBeforIn.setBenchunum( zero );
            flowerMaterialBeforIn.setBenchuprice( zero );
            flowerMaterialBeforIn.setBenchutotalprice( zero );
            flowerMaterialBeforIn.setJieyunum(zero);
            flowerMaterialBeforIn.setJieyuprice(zero);
            flowerMaterialBeforIn.setJieyutotalprice(zero);
            map.put(flowerMaterialBeforIn.getStorehouse() + "-" + flowerMaterialBeforIn.getCode(), flowerMaterialBeforIn);
        }

        for (FlowerMaterialCount flowerMaterialBeforout : queryBeforoutList) {
            FlowerMaterialCount flowerMaterialBeforoutNew = new FlowerMaterialCount();
            String key = flowerMaterialBeforout.getStorehouse() + "-" + flowerMaterialBeforout.getCode();
            FlowerMaterialCount flowerMaterialCountOld = map.get(key);
            BigDecimal befornum = BigDecimal.ZERO;
            BigDecimal beforTotalPrice = BigDecimal.ZERO;
            if (flowerMaterialCountOld != null) {
                befornum = flowerMaterialCountOld.getBefornum();
                beforTotalPrice = flowerMaterialCountOld.getBefortotalprice();
                BeanUtils.copyProperties(flowerMaterialCountOld, flowerMaterialBeforoutNew);
            }else{
                BeanUtils.copyProperties(flowerMaterialBeforout, flowerMaterialBeforoutNew);
            }
            //出
            BigDecimal befornum2 = flowerMaterialBeforout.getBefornum();
            BigDecimal beforTotalPrice2 = flowerMaterialBeforout.getBefortotalprice();

            BigDecimal befornumNew = befornum.subtract( befornum2 ) ;
            BigDecimal beforTotalPriceNew = beforTotalPrice.subtract(beforTotalPrice2 ) ;
            BigDecimal beforPriceNew = BigDecimal.ZERO;
            if(befornumNew.compareTo(BigDecimal.ZERO) !=0 ) {
                beforPriceNew = beforTotalPriceNew.divide(befornumNew, 2, BigDecimal.ROUND_HALF_UP);
            }
            flowerMaterialBeforoutNew.setBefornum( befornumNew );
            flowerMaterialBeforoutNew.setBeforprice( beforPriceNew );
            flowerMaterialBeforoutNew.setBefortotalprice( beforTotalPriceNew );
            map.put(key, flowerMaterialBeforoutNew);
        }
        //本期入库
        for (FlowerMaterialCount flowerMaterialCount : queryNowInList) {
            String key = flowerMaterialCount.getStorehouse() + "-" + flowerMaterialCount.getCode();
            FlowerMaterialCount flowerMaterialBeforoutNew = new FlowerMaterialCount();
            FlowerMaterialCount flowerMaterialCountOld = map.get(key);
            BigDecimal zero = BigDecimal.ZERO;
            if (flowerMaterialCountOld == null) {
                BeanUtils.copyProperties(flowerMaterialCount, flowerMaterialBeforoutNew);
                flowerMaterialBeforoutNew.setBefornum( zero );
                flowerMaterialBeforoutNew.setBeforprice( zero );
                flowerMaterialBeforoutNew.setBefortotalprice( zero );
            }else{
                BeanUtils.copyProperties(flowerMaterialCountOld, flowerMaterialBeforoutNew);
                flowerMaterialBeforoutNew.setBennum( flowerMaterialCount.getBennum() );
                flowerMaterialBeforoutNew.setBenprice( flowerMaterialCount.getBenprice() );
                flowerMaterialBeforoutNew.setBentotalprice( flowerMaterialCount.getBentotalprice() );
            }
            //出库
            flowerMaterialBeforoutNew.setBenchunum( zero );
            flowerMaterialBeforoutNew.setBenchuprice( zero );
            flowerMaterialBeforoutNew.setBenchutotalprice( zero );
            //结余
            flowerMaterialBeforoutNew.setJieyunum(zero);
            flowerMaterialBeforoutNew.setJieyuprice(zero);
            flowerMaterialBeforoutNew.setJieyutotalprice(zero);
            map.put(key, flowerMaterialBeforoutNew);
        }
        //本期出库
        for (FlowerMaterialCount flowerMaterialCount : queryNowOutList) {
            String key = flowerMaterialCount.getStorehouse() + "-" + flowerMaterialCount.getCode();
            FlowerMaterialCount flowerMaterialCountOld = map.get(key);
            FlowerMaterialCount flowerMaterialBeforoutNew = new FlowerMaterialCount();
            BigDecimal zero = BigDecimal.ZERO;
            if (flowerMaterialCountOld == null) {
                BeanUtils.copyProperties(flowerMaterialCount, flowerMaterialBeforoutNew);
                flowerMaterialBeforoutNew.setBefornum( zero);
                flowerMaterialBeforoutNew.setBeforprice( zero );
                flowerMaterialBeforoutNew.setBefortotalprice(zero );
                flowerMaterialBeforoutNew.setBennum(zero );
                flowerMaterialBeforoutNew.setBenprice(zero );
                flowerMaterialBeforoutNew.setBentotalprice( zero );
            }else{
                BeanUtils.copyProperties(flowerMaterialCountOld, flowerMaterialBeforoutNew);
                flowerMaterialBeforoutNew.setBenchunum(flowerMaterialCount.getBenchunum());
                flowerMaterialBeforoutNew.setBenchuprice(flowerMaterialCount.getBenchuprice());
                flowerMaterialBeforoutNew.setBenchutotalprice(flowerMaterialCount.getBenchutotalprice());
            }
            //结余
            flowerMaterialBeforoutNew.setJieyunum(zero);
            flowerMaterialBeforoutNew.setJieyuprice(zero);
            flowerMaterialBeforoutNew.setJieyutotalprice(zero);
            map.put(key, flowerMaterialBeforoutNew);
        }

        QueryWrapper<FlowerStorehouse> queryWrapper =new QueryWrapper<>();
        List<FlowerStorehouse> flowerStorehousesList = flowerStorehouseMapper.selectList(queryWrapper) ;
        Map<String,String> housemap =new HashMap<>();
        for(FlowerStorehouse flowerStorehouse :flowerStorehousesList){
            housemap.put(flowerStorehouse.getCode(), flowerStorehouse.getName());
        }


        List<FlowerMaterialCount> allList =new ArrayList<>();
        // 循环 map ，计算结余
        for( String  key  : map.keySet()) {
            BigDecimal zero = BigDecimal.ZERO;
            FlowerMaterialCount flowerMaterialCount = map.get(key);

            BigDecimal befornum = flowerMaterialCount.getBefornum();
            if (befornum == null) {
                befornum = BigDecimal.ZERO;
                flowerMaterialCount.setBefornum(befornum);
            }
            BigDecimal beforprice = flowerMaterialCount.getBeforprice();
            if (beforprice == null) {
                beforprice = BigDecimal.ZERO;
                flowerMaterialCount.setBeforprice(beforprice);
            }
            BigDecimal beforTotalPrice = flowerMaterialCount.getBefortotalprice();
            if (beforTotalPrice == null) {
                beforTotalPrice = BigDecimal.ZERO;
                flowerMaterialCount.setBefortotalprice(beforTotalPrice);
            }
            BigDecimal bennum = flowerMaterialCount.getBennum();
            if (bennum == null) {
                bennum = BigDecimal.ZERO;
                flowerMaterialCount.setBennum( bennum );
            }
            BigDecimal benprice = flowerMaterialCount.getBenprice();
            if (benprice == null) {
                benprice = BigDecimal.ZERO;
                flowerMaterialCount.setBenprice( benprice );
            }
            BigDecimal benTotalPrice = flowerMaterialCount.getBentotalprice();
            if (benTotalPrice == null) {
                benTotalPrice = BigDecimal.ZERO;
                flowerMaterialCount.setBentotalprice( benTotalPrice );
            }
            BigDecimal benchunum = flowerMaterialCount.getBenchunum();
            if (benchunum == null) {
                benchunum = BigDecimal.ZERO;
                flowerMaterialCount.setBenchunum( benchunum );
            }
            BigDecimal benchuprice = flowerMaterialCount.getBenchuprice();
            if (benchuprice == null) {
                benchuprice = BigDecimal.ZERO;
                flowerMaterialCount.setBenchuprice( benchuprice );
            }
            BigDecimal benchuTotalPrice = flowerMaterialCount.getBenchutotalprice();
            if (benchuTotalPrice == null) {
                benchuTotalPrice = BigDecimal.ZERO;
                flowerMaterialCount.setBenchutotalprice( benchuTotalPrice );
            }

            BigDecimal jieyunum = befornum.add(bennum).subtract(benchunum).setScale(2, BigDecimal.ROUND_HALF_UP);
            BigDecimal jieyuTotalPrice = beforTotalPrice.add(benTotalPrice).subtract(benchuTotalPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
            BigDecimal jieyuPrice = zero;
            if (jieyunum.compareTo(zero) != 0) {
                jieyuTotalPrice.divide(jieyunum, 2, BigDecimal.ROUND_HALF_UP);
            }
            flowerMaterialCount.setId(key);
            flowerMaterialCount.setJieyunum(jieyunum);
            flowerMaterialCount.setJieyuprice(jieyuPrice);
            flowerMaterialCount.setJieyutotalprice(jieyuTotalPrice);
            flowerMaterialCount.setStorehouse( housemap.get(flowerMaterialCount.getStorehouse()) );
            String dictcost = sysDictService.queryDictTextByKey("ch_cost", flowerMaterialCount.getCost());
            flowerMaterialCount.setCost( dictcost ) ;
            allList.add(flowerMaterialCount);
        }

        return allList;
    }
}
